/*
 * @Author: wwssaabb
 * @Date: 2021-11-23 09:15:38
 * @LastEditTime: 2021-11-23 09:35:54
 * @FilePath: \handwritten-code\array\filter.js
 */

//实现filter

Array.prototype.myFilter = function (fn, thisArg = globalThis) {
  if (typeof fn !== 'function') return new TypeError(`${fn} is not a function`)

  let res = []
  for (let i = 0; i < this.length; i++) {
    let result = fn.call(thisArg, this[i], i, this)
    if (result) res.push(this[i])
  }
  return res
}


let a = [{
    age: 18
  },
  {
    age: 19
  },
  {
    age: 28
  },
  {
    age: 8
  },
  {
    age: 38
  },
  {
    age: 8
  },
]

console.log(a.filter(i => i.age >= 18))
console.log(a.myFilter(i => i.age >= 18))